package com.taptech.doufu.services;

import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by lsy on 2015/8/17.
 */
public class CollectService {

    static final public int HANDLE_TYPE_ADD_TAG_COLLECT = 2001;
    static final public int HANDLE_TYPE_REMOVE_TAG_COLLECT = 2002;
    static final public int HANDLE_TYPE_LOAD_TAG_COLLECTS = 2003;
    static final public int HANDLE_TYPE_LOAD_TAG_TAKEIN = 2005;


    static CollectService collectService = null;

    private CollectService() {

    }

    static public CollectService getInstance() {
        if (collectService == null) {
            collectService = new CollectService();
        }
        return collectService;
    }

    /**
     * 添加标签收藏
     *
     * @param httpResponseListener
     * @param tagString
     */
    public synchronized void addCollectTagRequest(HttpResponseListener httpResponseListener, String tagString) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_ADD_TAG_COLLECT);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("tag", tagString);
        list.add(value);
        request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/" + "tags/collect");
        request.setListener(httpResponseListener);
        request.setData(list);
        HttpUtil.sendPostRequest(request);
    }

    /**
     * 移除标签收藏
     *
     * @param httpResponseListener
     * @param tagString
     */
    public synchronized void removeCollectTagRequest(HttpResponseListener httpResponseListener, String tagString) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_REMOVE_TAG_COLLECT);
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("tag", tagString);
        list.add(value);
        request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/" + "tags/uncollect");
        request.setListener(httpResponseListener);
        request.setData(list);
        HttpUtil.sendPostRequest(request);
    }

    /**
     * 拉取标签收藏列表
     *
     * @param httpResponseListener
     * @param last
     * @param pageSize20
     */
    public synchronized void loadCollectTagList(HttpResponseListener httpResponseListener, String last, int pageSize20) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_TAG_COLLECTS);
        request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/tags/mine" + "?last=" + last + "&size=" + Constant.PAGE_SIZE_20);
        request.setListener(httpResponseListener);
        HttpUtil.sendGetRequest(request);
    }

    public synchronized void loadTakeInTagList(HttpResponseListener httpResponseListener, String last) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_LOAD_TAG_TAKEIN);
        request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/tags/participated" + "?last=" + last + "&size=" + Constant.PAGE_SIZE_20);
        request.setListener(httpResponseListener);
        HttpUtil.sendGetRequest(request);
    }
}
